package com.recruit.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import java.io.IOException;
/****
 * author:丁文彬
 * time：2020.9.25
 */
/**
 * 编码集过滤器
 */
@WebFilter(filterName = "f1",urlPatterns = {"/*"},initParams = {@WebInitParam(name = "encode",value = "UTF-8")})
public class EncodeFilter implements Filter {

    private String encode;

    public void destroy() {
        System.out.println("EncodeFilter destroy");
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        System.out.println("EncodeFilter doFilter");

        //处理请求的编码集
        req.setCharacterEncoding(encode);
        //处理响应的编码集
        resp.setContentType("text/html;charset="+encode);
        //传递过滤链
        chain.doFilter(req, resp);
    }

    public void init(FilterConfig config) throws ServletException {
        encode = config.getInitParameter("encode");
        System.out.println("encode = "+encode);
        System.out.println("EncodeFilter init");
    }

}
